import React, {Component} from 'react'
import { Link } from 'react-router-dom'
import MUtil from 'util/mm.jsx'
const _mm = new MUtil()
import User from 'service/user-service.jsx'
const _user = new User();
class NavTop extends Component {
  constructor(props) {
    super(props)

    let username = _mm.getStorage('userInfo').username;
    if(username){
      this.state = {
        username: username
      }
    }else{
      window.location.href = '/login'
    }


  }
  onLogout(){
    _user.logout().then((res)=>{
      _mm.removeStorage('userInfo')
      window.location.href = '/login'
    },(errMsg)=>{
      _mm.errorTips(errMsg)
    })
  }

  render() {
    return (

      <div className="navbar navbar-default top-navbar" >
        <div className="navbar-header">

          <Link className="navbar-brand" to='/'><b>In</b>sight</Link>
        </div>

        <ul className="nav navbar-top-links navbar-right">

          <li className="dropdown">
            <a className="dropdown-toggle"  href="javascript:;" >
              <i className="fa fa-user fa-fw"></i>
              <span>欢迎，{this.state.username}</span>
              <i className="fa fa-caret-down"></i>
            </a>
            <ul className="dropdown-menu dropdown-user">


              <li><a onClick={ () =>{this.onLogout()}}><i className="fa fa-sign-out fa-fw"></i> 退出</a>
              </li>
            </ul>

          </li>

        </ul>
      </div>

    )
  }
}

export default NavTop